home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / tex / lametex_.z / lametex_ / lametex / src / Stack.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  4.2 KB  |  146 lines

  1. /* Stack.C
  2.  *
  3.  * Adjusting the look and feel of the document in a recursive way is done
  4.  * with a stack of Environments, where each environment holds variables such
  5.  * as the current font and margin sizes. This describes the stack.
  6.  *
  7.  * Copyright 1992 Jonathan Monsarrat. Permission given to freely distribute,
  8.  * edit and use as long as this copyright statement remains intact.
  9.  *
  10.  */
  11.  
  12. #include "Global.h"
  13. #include "Document.h"
  14. #include "Font.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. Environment *Stack::environments[MAXNESTING];
  20. int Stack::level;
  21.  
  22. Stack::Stack()
  23. {
  24.    level=0;
  25.    for(int x=0; x < MAXNESTING; x++)
  26.       environments[x] = NULL;
  27.    environments[0] = new Environment();  // Initialize the root environment
  28. }
  29.  
  30. void Stack::push(int, int, float, char *)
  31. {
  32.    if(level > MAXNESTING)
  33.       Global::files->fatal_error("Too much nesting");
  34.    
  35.    environments[level+1] = new Environment(environments[level]);
  36.    level++;
  37. }
  38.  
  39. void Stack::pop(int, int subtype, float, char *)
  40. {
  41.    if(level == 0)
  42.       Global::files->fatal_error("Too many \\end or } tokens");
  43.  
  44.    Stack::get(Environment::PDocument, subtype, "");
  45.  
  46.    environments[level-1]->revert(environments[level]);
  47.    delete environments[level];
  48.    level--;
  49. }
  50.  
  51. /* This functions sets a parameter in the current environment. When the '}'
  52.  * character or the \end command is encountered, the current environment
  53.  * will popped off the stack and this parameter will be reset to its former
  54.  * value (the value previous to the last '{' character or \begin command
  55.  * or the default value)
  56.  */
  57. void Stack::set(int paramtype, int subtype, float value, char *replacestr)
  58. {
  59.    environments[level]->set(paramtype,subtype,value,replacestr);
  60. }
  61.  
  62. /* This functions sets a parameter in the current environment, relative
  63.  * to its current value.
  64.  */
  65. void Stack::relative_set(int paramtype, int subtype, float value,
  66.              char *replacestr)
  67. {
  68.    environments[level]->
  69.       set(paramtype, subtype,
  70.        value + environments[level]->get(paramtype, subtype, replacestr),
  71.       replacestr);
  72. }
  73.  
  74.  
  75.  
  76. /* This function gets a parameter value from the current environment, 
  77.  * as indexed by the parameter type and subtype.
  78.  */
  79. float Stack::get(int paramtype, int subtype, char *comparestr)
  80. {
  81.    return environments[level]->get(paramtype, subtype, comparestr);
  82. }
  83.  
  84. void Stack::shutdown()
  85. {
  86.    char commandname[MAXSTRING];
  87.    if(level > 0)
  88.      Global::files->fatal_error("Missing \\end or }");
  89.    Global::files->outfile << endl;
  90.    Global::files->outfile << "ENDPAGE" << endl;
  91.  
  92.    Stack::get(Environment::PDocument, Document::ShutDown, "");
  93.    Global::files->outfile.close();
  94.    Global::labels->shutdown();
  95.    char realname[MAXSTRING];
  96.  
  97.    char srcdir[MAXSTRING];     // Take a trailing / off the source
  98.    strcpy(srcdir,SRCDIR);      // directory pathname, if needed.
  99.    int last = strlen(srcdir) - 1;
  100.    if(srcdir[last]=='/')
  101.       srcdir[last]='\0';
  102.    strcpy(realname, Global::files->outfilename);
  103.    char *p = strchr(realname,'.');
  104.    *p='\0';
  105.  
  106.    if(Global::files->plain_text_output) {
  107.     sprintf(commandname, "perl %s/plaintext.pl < %s > %s.txt", srcdir,
  108.         Global::files->outfilename, Global::files->outfileroot);
  109.     system(commandname);
  110.     sprintf(commandname, "Output has been placed in %s.txt",
  111.         Global::files->outfileroot);
  112.     cerr << commandname << endl;
  113.         sprintf(commandname,"rm %s", Global::files->outfilename);
  114.     system(commandname);
  115.       return;
  116.    }
  117.    Stack::get(Environment::PFont, Font::ShutDown, "");
  118.  
  119.    /* A number of files have been created. It's time to merge them
  120.       together into one big file */
  121.  
  122.  
  123.  
  124.    // Concatenate all these library and temporary files into
  125.    // the final rootname.ps file.
  126.  
  127.    sprintf(commandname,
  128.  "cat %s/latex.header lametex.PS %s/format.ps %s/breakpath.ps %s %s/latex.footer > %s.ps",
  129.        srcdir, srcdir, srcdir, Global::files->outfilename, srcdir,
  130.        realname);
  131.  
  132.    
  133.    system(commandname);   // Execute the concatenate command
  134.    cerr << "Created PostScript file " << realname
  135.     << ".ps" << endl;
  136.  
  137.    sprintf(commandname,"rm lametex.PS lametex.aux lametex.log %s", Global::files->outfilename);
  138.    system(commandname);   // Execute the concatenate command
  139. }
  140.  
  141. Length *Stack::get_length()
  142. {
  143.    return (Length *)environments[level]->get_param(Environment::PLength);
  144. }
  145.  
  146.